package com.xuexisheqv.subject.domain.handler.subject;

import com.xuexisheqv.subject.common.enums.IsDeletedFlagEnum;
import com.xuexisheqv.subject.common.enums.SubjectInfoTypeEnum;
import com.xuexisheqv.subject.domain.convert.JudgeSubjectConverter;
import com.xuexisheqv.subject.domain.entity.SubjectAnswerBO;
import com.xuexisheqv.subject.domain.entity.SubjectInfoBO;
import com.xuexisheqv.subject.domain.entity.SubjectOptionBO;
import com.xuexisheqv.subject.infra.basic.entity.SubjectInfo;
import com.xuexisheqv.subject.infra.basic.entity.SubjectJudge;
import com.xuexisheqv.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 判断题目策略类
 */

@Component
public class JudgeTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectJudgeService subjectJudgeService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        // 判断题目的插入
        if(subjectInfoBO == null){
            return;
        }
        List<SubjectJudge> subjectJudgeList = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option ->{
            // 转换为判断题
            SubjectJudge subjectJudge = JudgeSubjectConverter.INSTANCE.convertBoToEntity(option);
            subjectJudge.setSubjectId(subjectInfoBO.getId());
            subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            // 不用单独设置 correct,option里面本来就有，
            subjectJudgeList.add(subjectJudge);
        });
        subjectJudgeService.batchInsert(subjectJudgeList);
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(Long.valueOf(subjectId));
        List<SubjectJudge> result = subjectJudgeService.queryByConditon(subjectJudge);
        List<SubjectAnswerBO> subjectAnswerBO = JudgeSubjectConverter.INSTANCE.convertEntityToAnswerBoList(result);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBO);
        return subjectOptionBO;

    }


}
